package controllers

import (
	beego "github.com/beego/beego/v2/server/web"
	"video/models"
)

type VideoController struct {
	beego.Controller
}

type Json struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (c *VideoController) Add() {
	title := c.GetString("title")
	intro := c.GetString("intro")
	img := c.GetString("img")
	timed := c.GetString("timed")
	people := c.GetString("people")

	video := models.Video{
		Title:  title,
		Intro:  intro,
		Img:    img,
		Timed:  timed,
		People: people,
	}

	err := models.DB.Create(&video).Error
	if err != nil {
		c.Data["json"] = Json{
			Code: 0,
			Msg:  "添加失败",
			Data: nil,
		}
		c.ServeJSON()
	} else {
		c.Data["json"] = Json{
			Code: 200,
			Msg:  "添加成功",
			Data: nil,
		}
		c.ServeJSON()
	}
}

func (c *VideoController) List() {
	var video []models.Video
	err := models.DB.Find(&video).Error
	if err != nil {
		c.Data["json"] = Json{
			Code: 0,
			Msg:  "查询失败",
			Data: nil,
		}
		c.ServeJSON()
	} else {
		c.Data["json"] = Json{
			Code: 0,
			Msg:  "查询成功",
			Data: video,
		}
		c.ServeJSON()
	}
}

func (c *VideoController) UP() {
	title := c.GetString("title")
	intro := c.GetString("intro")
	img := c.GetString("img")
	timed := c.GetString("timed")
	people := c.GetString("people")

	video := models.Video{
		Title:  title,
		Intro:  intro,
		Img:    img,
		Timed:  timed,
		People: people,
	}

	err := models.DB.Save(&video).Error
	if err != nil {
		c.Data["json"] = Json{
			Code: 0,
			Msg:  "修改失败",
			Data: nil,
		}
		c.ServeJSON()
	} else {
		c.Data["json"] = Json{
			Code: 200,
			Msg:  "修改成功",
			Data: nil,
		}
		c.ServeJSON()
	}
}
